﻿<template>
  <a-modal
    :title="title"
    width="40%"
    :visible="visible"
    :confirmLoading="loading"
    @ok="handleSubmit"
    @cancel="()=>{this.visible=false}"
  >
    <a-spin :spinning="loading">
      <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
        <a-form-model-item label="主表Id" prop="MainId">
          <a-input v-model="entity.MainId" autocomplete="off" placeholder="主表Id" />
        </a-form-model-item>
        <a-form-model-item label="存货Id" prop="InvId">
          <a-input v-model="entity.InvId" autocomplete="off" placeholder="存货Id" />
        </a-form-model-item>
        <a-form-model-item label="存货编号" prop="InvCode">
          <a-input v-model="entity.InvCode" autocomplete="off" placeholder="存货编号" />
        </a-form-model-item>
        <a-form-model-item label="存货名称" prop="InvName">
          <a-input v-model="entity.InvName" autocomplete="off" placeholder="存货名称" />
        </a-form-model-item>
        <a-form-model-item label="存货规格型号" prop="InvStd">
          <a-input v-model="entity.InvStd" autocomplete="off" placeholder="存货规格型号" />
        </a-form-model-item>
        <a-form-model-item label="平均单价" prop="AvgPrice">
          <a-input v-model="entity.AvgPrice" autocomplete="off" placeholder="平均单价" />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
export default {
  props: {
    parentObj: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  data () {
    return {
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 18 }
      },
      visible: false,
      loading: false,
      entity: {},
      rules: {},
      title: ''
    }
  },
  methods: {
    init () {
      this.visible = true
      this.entity = {}
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    openForm (id, title) {
      this.init()
      this.title = title
      if (id) {
        this.loading = true
        this.$http.post('/CostManagement/cm_productionsub/GetTheData', { id: id }).then(resJson => {
          this.loading = false
          this.entity = resJson.Data
        })
      }
    },
    handleSubmit () {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        this.loading = true
        this.$http.post('/CostManagement/cm_productionsub/SaveData', this.entity).then(resJson => {
          this.loading = false
          if (resJson.Success) {
            this.$message.success('操作成功!')
            this.loading = false
            this.visible = false
	        this.$emit('returnMain')
          } else {
            this.$message.error(resJson.Msg)
            this.loading = false
          }
        })
      })
    },
    // 非空校验
    validEmpty (index, name, tips) {
      return (rule, value, callback) => {
        const data = this.DetaiData[index][name]
        if (!data) {
          this.$message.warning('列表第' + (index + 1) + '行：' + tips)
          callback(tips || '必填')
        } else {
          callback()
        }
      }
    }
  }
}
</script>
<style scoped>
</style>
